#!/bin/sh

################################################################################
# Copyright 1999-2010 by Nice, srl.,
# Via Marchesi di Roero, 1
# 14020 Cortanze (AT) - ITALY
# All rights reserved.
#
# This software is the confidential and proprietary information
# of Nice, srl. ("Confidential Information").
# You shall not disclose such Confidential Information
# and shall use it only in accordance with the terms of
# the license agreement you entered into with Nice.
################################################################################

#------------------------------------------------------------------
# Remove files
#
# Usage: fm.remove <path> <files>
#
# - path:  the current path (relative to vroot)
# - files: files to be removed (one per line)
#
#------------------------------------------------------------------

. "${EF_ROOT}/plugins/irods/bin/common"
. "${EF_ROOT}/plugins/fm/fm/backends/common"

if [ -z "$1" ]; then
  fatal "Cannot remove the selected file(s)" "Missing input parameter: current path"
fi

if [ -z "$2" ]; then
    # No file to be deleted
    exit 0
fi

#_fullpath="${FM_VROOT_PATH}$1"
_fullpath="${1}"

# Change to the required directory
${IRODS_ICOMMANDS_DIR}/icd "${_fullpath}" >/dev/null 2>&1
if [ $? -ne 0 ]; then
    fatal "Cannot remove the selected file(s)" "Cannot access to working directory"
fi


echo "${2}" | while read fileitem ; do
  if [ -n "${fileitem}" ] ; then
    _message="`${IRODS_ICOMMANDS_DIR}/irm -r \"${fileitem}\" 2>&1 || return 1`"
    if [ $? -ne 0 ]; then
      # FIXME: we need to generate better error messages
      error "An error occurred removing the selected file(s)" "The following files were not removed: `list_files \"${_message}\"`"
    fi
  else
    error "Sorry but the selected file '${fileitem}' does not exist."
  fi
done


# ex:ts=2:et:

